const Mock = require("mockjs");
const data = Mock.mock({
    'counts': '@integer(100, 5000)',
    'pagesize': 10,
    'pages': '',
    'page': 1,
    ['items|300']: [{
        'id': '@character("lower")' + '@natural(10000)',
        'addTime': '@datetime',
        'username': '@cname',
        'questionIDs|3': [
            {
                'questionid|+1': [31,
                    30,
                    29,
                    26,
                    25,
                    24,
                    21,
                    19,
                    18,],
                'questionNum': '@character("lower")' + '@natural(10000)'
            }
        ],
        'progressOfAnswer': '@integer(10, 100)',
        'accuracyRate': '@integer(10, 100)',
        'totalSeconds': '@integer(100, 5000)',
        'questionType|+1': ['单选', '多选', '简答'],
    }]
})
module.exports = [
    // 获取列表
    {
        url: 'questions/randoms/',
        type: 'get',
        response: config => {
            const items = data.items
            const { id, page, pagesize } = config.query
            console.log(config.query);
            const mockList = items.filter(user => {
                if (id && user.id.indexOf(id) === -1) return false
                return true
            })
            const pageList = mockList.filter((item, index) => index < pagesize * page && index >= pagesize * (page - 1))

            return {
                code: 20000,
                data: {
                    total: mockList.length,
                    items: pageList
                }
            }
        }
    },
    // 创建or编辑
    {
        url: '/admin/createHospital',
        type: 'post',
        response: config => {
            let obj = config.query
            if (obj.id) {
                data.items.some(u => {
                    if (u.id === obj.id) {
                        u.id = obj.id
                        u.description = obj.description
                        u.status = obj.status
                        return true
                    }
                })
                return {
                    code: 20000,
                    message: '修改成功'
                }
            } else {
                obj.id = Mock.Random.id()
                obj.created_at = Mock.mock('@now')
                data.items.unshift(obj)

                return {
                    code: 20000,
                    message: '添加成功'
                }
            }

        }
    },
    // 删除
    {
        url: 'questions/randoms/',
        type: 'delete',
        response: config => {
            let obj = config.query
            if (!obj.id) {
                return {
                    code: -999,
                    message: '参数不正确'
                }
            } else {
                data.items = data.items.filter(u => u.id !== obj.id)
                return {
                    code: 20000,
                    message: '删除成功'
                }
            }
        }
    },
    // 批量删除
    {
        url: '/admin/batchremove/hospital',
        type: 'post',
        response: config => {
            let str = config.query.idStr
            let arr = str.split(',')
            data.items = data.items.filter(u => !arr.includes(u.id))
            return {
                code: 20000,
                message: '批量删除成功'
            }
        }
    }

]

